using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Threading;
using TrackMe.Apps.VM;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;


namespace TrackMe.View
{
	[Export(typeof(IWidgetView))]
    public partial class WidgetWindow : IWidgetView
    {
    	private readonly NotifyIcon notifyIcon;
    	private readonly MenuItem notifyShowHideMenuItem;
		private readonly DispatcherTimer autoHideTimer;


		private WidgetVM VM { get { return (WidgetVM) DataContext; }
		}

    	public WidgetWindow()
    	{
            InitializeComponent();

            notifyIcon = new NotifyIcon
            	{
            		Text = "TimeTracker",
            		Icon = new System.Drawing.Icon(@"Images\clock_16.Ico"),
            		Visible = true,
            		ContextMenu = new ContextMenu()
            	};
            notifyShowHideMenuItem = new MenuItem("Hide", ShowHideNotifyMenuItemClick);
            notifyIcon.ContextMenu.MenuItems.Add(notifyShowHideMenuItem);
            notifyIcon.ContextMenu.MenuItems.Add("Exit", ExitNotifyMenuItemClick);
            notifyIcon.BalloonTipTitle = "TimeTracker";
            notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
			notifyIcon.MouseMove += delegate
			{
				Visibility = Visibility.Visible;
				Mouse.GetPosition(this);
			};

            notifyIcon.MouseClick +=
                delegate(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        ContextMenu.Visibility = Visibility.Visible;
                    }
                };
            notifyIcon.DoubleClick += (sender, args) => ToggleVisibility();

			autoHideTimer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(100)};
    		autoHideTimer.Tick += delegate
    		{
    		    Visibility = Visibility.Hidden;
    			autoHideTimer.IsEnabled = false;
    		};

    	}

    	protected void WindowMouseDoubleClick(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
        }

        protected void WindowClosing(object sender, CancelEventArgs e)
        {
            App.CurrentApp.GracefulShutDown();
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }

    	protected void WindowKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyboardDevice.IsKeyDown(Key.Escape))
            {
                Hide();
            }
        }

    	protected void ExitNotifyMenuItemClick(object sender, EventArgs e)
        {
            App.CurrentApp.GracefulShutDown();
        }

        protected void ToggleVisibility()
        {
            if (IsVisible)
            {
                Hide();
                notifyShowHideMenuItem.Text = "Show";
            }
            else
            {
                Show();
                notifyShowHideMenuItem.Text = "Hide";
            }
            if (!string.IsNullOrEmpty(notifyIcon.BalloonTipText))
            {
                notifyIcon.ShowBalloonTip(1000);
            }
        }

        protected void ShowHideNotifyMenuItemClick(object sender, EventArgs e)
        {
            ToggleVisibility();
        }

		protected void ExitMenuItemClick(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.GracefulShutDown();
        }

        protected void HideMenuItemClick(object sender, RoutedEventArgs e)
        {
            Hide();
        }

    	protected void UpdateNotifyIcon()
        {
            string result = "TimeTracker: No task";
			if (VM.CurrentTask != null)
			{
				result = "You're " + VM.CurrentTask.TaskName + " since ";// +VM.CurrentJob.StartTime.ToShortTimeString();
            }
            notifyIcon.BalloonTipText = result;
            result = "TimeTracker: " + result;
            if (result.Length >= 63)
            {
                result = result.Substring(result.Length - 63);
            }
            notifyIcon.Text = result;
        }

		private void WindowMouseLeave(object sender, MouseEventArgs e)
		{
			autoHideTimer.IsEnabled = true;
		}

		private void WindowMouseEnter(object sender, MouseEventArgs e)
		{
			autoHideTimer.IsEnabled = false;
		}

    	private void WidgetWindow_OnSizeChanged(object sender, SizeChangedEventArgs e)
    	{
			Left = SystemParameters.WorkArea.Right - Width;
			Top = SystemParameters.WorkArea.Bottom - e.NewSize.Height;
    	}


		public void SetCreateView(object view)
		{
            CreateSpot.Children.Add((UIElement)view);
		}

	    public void SetSearchView(object view)
	    {
            SearchSpot.Children.Add((UIElement)view);
        }

	    public void SetRecentView(object view)
	    {
            RecentSpot.Children.Add((UIElement)view);
        }

		public object GetChildVM(object selectedTab)
		{
			if (selectedTab == RecentSpot.Parent)
				return ((FrameworkElement)RecentSpot.Children[0]).DataContext;
			return null;
		}

		private void ButtonExitClick(object sender, RoutedEventArgs e)
		{
			App.CurrentApp.GracefulShutDown();
		}

    }
}